package clientapp;

    

      import java.io.IOException;   
      import java.io.OutputStream;
      import java.util.Vector; 
      import javax.bluetooth.DeviceClass;
      import javax.bluetooth.DiscoveryAgent;
      import javax.bluetooth.DiscoveryListener;
      import javax.bluetooth.LocalDevice;
      import javax.bluetooth.RemoteDevice;
      import javax.bluetooth.ServiceRecord;
      import javax.bluetooth.UUID;
      import javax.microedition.io.Connector;
      import javax.microedition.io.StreamConnection;
  
       
  
      /**
  
      * 
  
      */
  
      public class clientSenderSPPtest implements Runnable, DiscoveryListener{
  
         
  
          //object used for waiting
  
          private static final Object lock=new Object();
         // private static String connectionURL= "btspp://000A3A65AD37:1101";
          private static String tekst = "testje vanuit client";
          //vector containing the devices discovered
          private static Vector vecDevices=new Vector();
          private static String connectionURL=null;
 
          //methods of DiscoveryListener
 
          public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
 
              //add the device to the vector
 
              if(!vecDevices.contains(btDevice)){
 
                  vecDevices.addElement(btDevice);
 
              }
 
          }
 
       
 
          //implement this method since services are not being discovered
 
          public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
 
              if(servRecord!=null && servRecord.length>0){
 
                  connectionURL=servRecord[0].getConnectionURL(0,false);
 
              }
 
              synchronized(lock){
 
                  lock.notify();
 
              }
 
          }
 
       
 
         //implement this method since services are not being discovered
 
          public void serviceSearchCompleted(int transID, int respCode) {
 
              synchronized(lock){
 
                  lock.notify();
 
              }
 
          }
   
 
          public void inquiryCompleted(int discType) {
 
              synchronized(lock){
 
                  lock.notify();
 
 }
          }//end method

    public void run() {
        try {
            clientSenderSPPtest client = new clientSenderSPPtest();
            //display local device address and name
            LocalDevice localDevice = LocalDevice.getLocalDevice();

            //find devices
            DiscoveryAgent agent = localDevice.getDiscoveryAgent();

            agent.startInquiry(DiscoveryAgent.GIAC, client);
            try {
                synchronized (lock) {
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            //print all devices in vecDevices
            int deviceCount = vecDevices.size();
            if (deviceCount <= 0) {
                
               
            } else {
                //print bluetooth device addresses and names in the format [ No. address (name) ]
                
                for (int i = 0; i < deviceCount; i++) {
                    RemoteDevice remoteDevice = (RemoteDevice) vecDevices.elementAt(i);
                }
            }
            //check for spp service
        //    String adres1 ="000A3A65AD37";
          //  Object v = adres1;
            //vecDevices.addElement(v);
            //System.out.println(vecDevices);

            RemoteDevice remoteDevice = (RemoteDevice) vecDevices.elementAt(0);
            UUID[] uuidSet = new UUID[1];
            uuidSet[0] = new UUID("1101", false);
            
            agent.searchServices(null, uuidSet, remoteDevice, client);
            try {
                synchronized (lock) {
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (connectionURL == null) {
                
                System.exit(0);
            }
            //connect to the server and send a line of text
            StreamConnection streamConnection = (StreamConnection) Connector.open(connectionURL);
            //send string
            OutputStream outStream = null;
            byte[] test = tekst.getBytes();
            try {
                outStream = streamConnection.openOutputStream();
                outStream.write(test);
                outStream.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }











    }

      }

